package com.szholly.pro.watergas_server.ps.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.data.ListPageResult;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.common.web.WebContext;
import com.szholly.pro.watergas_server.ps.service.DataReportService;
import com.szholly.pro.watergas_server.ps.service.FourTuple;
import com.szholly.pro.watergas_server.ps.service.OnlineMonitorService;
import com.szholly.pro.watergas_server.ps.service.ThreeTuple;
import com.szholly.pro.watergas_server.ps.service.TwoTuple;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/ExportImage/*")
public class ExportImageController extends BaseController {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	DataReportService dataReportService;
	
	@Autowired
	OnlineMonitorService onlineMonitorService;

	@RequestMapping(value = "/RealTime")
	public void RealTime(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String wwtpId = RequestUnit.getString("WWTP_ID");
		String parmToChecks = RequestUnit.getString("parmToChecks");
		String station_id = RequestUnit.getString("STATION_ID");
		String start = RequestUnit.getString("start");// start date
		String end = RequestUnit.getString("end");// end date
		String mn = RequestUnit.getString("mn");
		String waterType = RequestUnit.getString("WATER_TYPE");
		String[] parms = parmToChecks.split(",");
		
		Map<String,String> params = new HashMap<>();
		
		if(StringUtil.IsNotNullOrEmpty(wwtpId)){
			params.put("wwtp_id", wwtpId);
		}
		if(StringUtil.IsNotNullOrEmpty(parmToChecks)){
			params.put("parmToChecks", parmToChecks);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(mn)){
			params.put("mn", mn);
		}
		if(StringUtil.IsNotNullOrEmpty(start)){
			params.put("start", start);
		}
		if(StringUtil.IsNotNullOrEmpty(end)){
			params.put("end", end);
		}
		if(StringUtil.IsNotNullOrEmpty(waterType)){
			params.put("waterType", waterType);
		}
		
		if(StringUtil.IsNullOrEmpty(station_id) && StringUtil.IsNotNullOrEmpty(mn)){
			station_id = RedisHelper.getInstance().getStationCodeByMN(mn);
		}
		
		ListPageResult result = dataReportService.getRealTimeData(params, null);
		JSONObject resultJson = DataUtil.listToJSONObject(result.getList());
		JSONObject FZ = dataReportService.getAlarmInfo(mn,parms);
		resultJson.put("limitData", FZ);
		
		String parmsJosnString = JSONArray.fromObject(parms).toString();
		resultJson.put("parms", parmsJosnString);
		resultJson.put("pageInfo", result.getPageInfo());
		
		outString(resultJson.toString());
	}
	
	/**
	 * 数据一致性
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="queryCoherencedata")
	public void queryCoherencedata(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params=new HashMap<>();
		HashMap<String,Object> data=new HashMap<>();
		String start=RequestUnit.getString("start");
		String end=RequestUnit.getString("end");
		String station_id=RequestUnit.getString("STATION_ID");
		String username=RequestUnit.getString("username");
		if(StringUtil.IsNotNullOrEmpty(username)){
			params.put("username", username);
		}
		if(StringUtil.IsNotNullOrEmpty(start)){
			params.put("start", start);
		}
		if(StringUtil.IsNotNullOrEmpty(end)){
			params.put("end", end);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		List<Object> dataItem=dataReportService.queryCoherencedata(params);
		data.put("ItemList", dataItem);
		String result = JsonUtil.ConvertObjectToJson(data);
		outString(result);
	}
	/**
	 * 数据一致性导出
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="exportCoherencedataPDF")
	public void exportCoherencedataPDF(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params=new HashMap<>();
		HashMap<String,Object> data=new HashMap<>();
		String start=RequestUnit.getString("start");
		String end=RequestUnit.getString("end");
		String station_id=RequestUnit.getString("STATION_ID");
		String username=RequestUnit.getString("username");
		String checkData = RequestUnit.getParameter("checkData");
		if(StringUtil.IsNotNullOrEmpty(start)){
			params.put("start", start);
		}
		if(StringUtil.IsNotNullOrEmpty(username)){
			params.put("username", username);
		}
		if(StringUtil.IsNotNullOrEmpty(end)){
			params.put("end", end);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		List<Object> dataItem=dataReportService.exportCoherencedata(params);
		data.put("ItemList", dataItem);
		data.put("start", start);
		data.put("end", end);	
		System.out.println("checkData:"+checkData);
		if(checkData != null && !checkData.equals("")&& !checkData.equals("null")){
			String jsonResult = JsonUtil.ConvertArrayToJson(dataItem);
			outString(jsonResult);
		}
		else{
			outPDF(data,"Coherencedata.html","数据一致性报表.pdf",false);
		}
	}
	
	/**
	 * 获取所有站点的实时数据
	 * gislin
	 * 2016年5月7日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/AllStationRealTimeData")
	public void AllStationRealTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String parmToChecks = RequestUnit.getString("parmToChecks");
		String waterType = RequestUnit.getString("WATER_TYPE");
		
		HashMap<String, Object> list = dataReportService.AllStationRealTimeData(waterType, parmToChecks);
		
		String result = DataUtil.objectToJsonObject(list).toString();
		outString(result);
	}
	
	/**
	 * 查询浓度历史数据
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/HistoricalData")
	public void historicalData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		long start = System.currentTimeMillis();
		Map<String,String> params = getHistoricalParams();
		String mn = params.get("mn");
		List<RowBase> list = dataReportService.getHistoricalData(params, null);
		JSONObject resultJson = DataUtil.listToJSONObject(list, null, null);
		
		long end = System.currentTimeMillis();
		logger.info("查询历史数据分耗时 {} 毫秒", end-start);
		String parmToChecks = params.get("parmToChecks");
		String[] parms = parmToChecks.split(",");
		String parmsJosnString = JSONArray.fromObject(parms).toString();
		resultJson.put("parms", parmsJosnString);
		
		JSONObject FZ = dataReportService.getAlarmInfo(mn,parms);
		resultJson.put("limitData", FZ);
		outString(resultJson.toString());
		
		end = System.currentTimeMillis();
		logger.info("查询历史数据总耗时 {} 毫秒", end-start);
	}
	/**
	 * 查询通量历史数据
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/historicalFluxData")
	public void historicalFluxData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		long start = System.currentTimeMillis();
		Map<String,String> params = getHistoricalParams();
		String mn = params.get("mn");
		String station_id = RedisHelper.getInstance().getStationCodeByMN(mn);
		List<RowBase> list = dataReportService.getHistoricalDataCou(params, null);
		JSONObject resultJson = DataUtil.listToJSONObject(list, null, null);
		
		long end = System.currentTimeMillis();
		logger.info("查询历史数据分耗时 {} 毫秒", end-start);
		String parmToChecks = params.get("parmToChecks");
		String[] parms = parmToChecks.split(",");
		String parmsJosnString = JSONArray.fromObject(parms).toString();
		resultJson.put("parms", parmsJosnString);
		
		JSONObject FZ = dataReportService.getAlarmInfoCou(station_id);
		resultJson.put("limitData", FZ);
		outString(resultJson.toString());
		
		end = System.currentTimeMillis();
		logger.info("查询历史数据总耗时 {} 毫秒", end-start);
	}
	/**
	 * 通量数据导出
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportHistoricalCouExcel")
	public void exportHistoricalCouExcel(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getHistoricalParams();
		
		List<RowBase> rows = dataReportService.getHistoricalDataCou(params, null);
		FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> list
			= new FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>>(rows,null,null,null);
		
		responseExcel(null,list);
	}
	/**
	 * 历史数据导出
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportHistoricalExcel")
	public void exportHistoricalExcel(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getHistoricalParams();
		
		List<RowBase> rows = dataReportService.getHistoricalData(params, null);
		FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> list
			= new FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>>(rows,null,null,null);
		
		responseExcel(null,list);
	}

	private Map<String,String> getHistoricalParams(){
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String flow_type = RequestUnit.getString("FLOW_TYPE");
		String station_id = RequestUnit.getString("STATION_ID");
		String start = RequestUnit.getString("START");// start date
		String end = RequestUnit.getString("END");// end date
		String dataType = RequestUnit.getString("END");// 排放量或者浓度均值
		String dataSource = RequestUnit.getString("DATA_SOURCE");// data
																// source:1:real
																// time,2:minute,3:hour,4:day
		String parmToChecks = RequestUnit.getString("parmToChecks");
		String isForModify = RequestUnit.getString("isForModify");
		String mn = RequestUnit.getString("mn");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			params.put("flow_type", flow_type);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(start)){
			params.put("start", start);
		}
		if(StringUtil.IsNotNullOrEmpty(end)){
			params.put("end", end);
		}
		if(StringUtil.IsNotNullOrEmpty(dataSource)){
			params.put("dataSource", dataSource);
		}
		if(StringUtil.IsNotNullOrEmpty(parmToChecks)){
			params.put("parmToChecks", parmToChecks);
		}
		if(StringUtil.IsNotNullOrEmpty(mn)){
			params.put("mn", mn);
		}
		if(StringUtil.IsNotNullOrEmpty(dataType)){
			params.put("dataType", dataType);
		}
		if(StringUtil.IsNotNullOrEmpty(isForModify)){
			params.put("isForModify", isForModify);
		}
		
		return params;
	}
	
	/**
	 * 获取报表数据通用方法
	 * gislin
	 * 2016年3月22日
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value = "/getReport")
	public void getReport(HttpServletRequest resq, HttpServletResponse res ){
		Map<String,String> params = getReportParams();
		
		FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> rows = dataReportService.getReport(params);
		
		response(params,rows);
	}
	
	/**
	 * 获取报表通量数据通用方法
	 * gislin
	 * 2016年3月22日
	 * @param resq
	 * @param res
	 */
	@RequestMapping(value = "/getCOUReport")
	public void getCOUReport(HttpServletRequest resq, HttpServletResponse res ){
		Map<String,String> params = getReportParams();
		
		FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> rows = dataReportService.getReport(params);
		
		response(params,rows);
	}
	
	// 导出报表通用方法
	@RequestMapping(value = "/exportReportExcel")
	public void exportReportExcel(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getReportParams();	
		FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> rows = dataReportService.getReport(params);
		responseExcel(params,rows);
	}

	private Map<String, String> getReportParams() {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String flow_type = RequestUnit.getString("FLOW_TYPE");
		String station_id = RequestUnit.getString("STATION_ID");
		String water_type = RequestUnit.getString("WATER_TYPE");
		String startDate = RequestUnit.getString("startDate");
		String endDate = RequestUnit.getString("endDate");
		String parmToChecks = RequestUnit.getString("parmToChecks");
		String mn = RequestUnit.getParameter("mn");
		String dataType = RequestUnit.getParameter("dataType");
		String isCOU = RequestUnit.getParameter("isCOU");
		String parmsToChar = RequestUnit.getParameter("parmsToChar");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(isCOU)){
			params.put("isCOU", isCOU);
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			params.put("flow_type", flow_type);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		else if(StringUtil.IsNotNullOrEmpty(mn)){
			station_id = RedisHelper.getInstance().getStationCodeByMN(mn);
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(water_type)){
			params.put("water_type", water_type);
		}
		if(StringUtil.IsNotNullOrEmpty(parmToChecks)){
			params.put("parmToChecks", parmToChecks);
		}
		if(StringUtil.IsNotNullOrEmpty(mn)){
			params.put("mn", mn);
		}
		if(StringUtil.IsNotNullOrEmpty(dataType)){
			params.put("dataType", dataType);
		}
		if(StringUtil.IsNotNullOrEmpty(startDate)){
			params.put("startDate", startDate);
		}
		if(StringUtil.IsNotNullOrEmpty(endDate)){
			params.put("endDate", endDate);
		}
		if(StringUtil.IsNotNullOrEmpty(parmsToChar)){
			params.put("parmsToChar", parmsToChar);
		}
		return params;
	}

	private void response(Map<String,String> params,FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> rows){
		
		JSONObject resultJson = DataUtil.listToJSONObject(rows.first);
		
		String parmToChecks = params.get("parmToChecks");
		String mn = params.get("mn");
		String[] parms = parmToChecks.split(",");
		String parmsJosnString = JSONArray.fromObject(parms).toString();
		resultJson.put("parms", parmsJosnString);
		
		JSONObject FZ = dataReportService.getAlarmInfo(mn,parms);
		resultJson.put("limitData", FZ);
		
		JSONArray statistic = JSONArray.fromObject(rows.second, JsonUtil.getConfig());
		resultJson.put("statistic", statistic);
		
		if(rows.third!=null){
			JSONObject itemClass = JSONObject.fromObject(rows.third.first, JsonUtil.getConfig());
			JSONObject totalClass = JSONObject.fromObject(rows.third.second, JsonUtil.getConfig());
			JSONObject mainPollutant = JSONObject.fromObject(rows.third.third, JsonUtil.getConfig());
			resultJson.put("item", itemClass);
			resultJson.put("total", totalClass);
			resultJson.put("pollutant", mainPollutant);
		}
		if (rows.fourth!=null) {
			JSONObject index = JSONObject.fromObject(rows.fourth.second, JsonUtil.getConfig());
			resultJson.put("comprehensivePolluteIndex",index);
		}
		
		outString(resultJson.toString());
	}
	private void responseExcel(Map<String,String> params,FourTuple<List<RowBase>, List<RowBase>, ThreeTuple<RowBase, RowBase, RowBase>,TwoTuple<RowBase, RowBase>> rows) throws Exception{
		String parmToChecks = params.get("parmToChecks");
		String[] parms = parmToChecks.split(",");
		if(rows == null){
			return;
		}
		List<RowBase> list = new ArrayList<>();
		
		if(rows.first!=null){
			list.addAll(rows.first);
		}
		
		if(rows.second!=null){
			list.addAll(rows.second);
		}
		
		Map<String,Object> data = new HashMap<>();
		data.put("parms",parms);
		data.put("list", list);
		
		List<String> headers = Arrays.asList(RequestUnit.getParameter("HEADERS").split(","));
		data.put("headers", headers);
		
		List<String> fields = Arrays.asList(RequestUnit.getParameter("FIELDS").split(","));
		data.put("fields", fields);
		
		String title = RequestUnit.getParameter("TITLE");
		if(StringUtil.IsNotNullOrEmpty(title)){
			data.put("title",title);
			data.put("titleSpan", fields.size());
		}
		String name = RequestUnit.getParameter("NAME");
		if(StringUtil.IsNotNullOrEmpty(name)){
			data.put("name",name);
			data.put("nameSpan", fields.size()-fields.size()/2);
		}
		String time = RequestUnit.getParameter("TIME");
		if(StringUtil.IsNotNullOrEmpty(time)){
			data.put("time",time);
			data.put("timeSpan", fields.size()/2);
		}
		
		if(rows.third!=null){
			data.put("item", rows.third.first);
			data.put("comprehensive", rows.third.second);
			data.put("pollutant", rows.third.third);
		}
		//导出中新加入综合污染指数
		if (rows.fourth !=null) {
			data.put("comprehensivePolluteIndex",rows.fourth.second);
		}
		//新添加的html模板，包括导出数据和图片
		String template = "DataReport.html";
		String fileName = RequestUnit.getParameter("FILENAME");
		HttpServletRequest resq = WebContext.getRequest();
		HttpServletResponse res = WebContext.getResponse();
		Configuration cfg = new Configuration();
		String tempPath = resq.getServletContext().getRealPath("/") + "WEB-INF/freeMarkerTemp/";
		cfg.setDirectoryForTemplateLoading(new File(tempPath));
		Template temp = cfg.getTemplate(template, "UTF-8");
		//输出填充数据后的html文件存放位置   
		OutputStream outputStream = new ByteArrayOutputStream();		
		OutputStreamWriter writer = new OutputStreamWriter(outputStream);
		String downloadfilename = toUtf8String(WebContext.getRequest(), fileName);
		try {
			res.setContentType("application/x-msdownload;charset=utf-8");
			//解决火狐也乱码问题
			res.setHeader("Content-Disposition", "attachment; filename=\"" + downloadfilename + "\"");			
			temp.process(data, writer);
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		writer.flush();
	}
	
	@RequestMapping(value = "/getMonitorData")
	public void getMonitorData(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String wwtpId = RequestUnit.getString("wwtpid");
		String startDateTime = RequestUnit.getString("startDateTime");
		String endDateTime = RequestUnit.getString("endDateTime");

		String strSQL = "select st.WWTP_ID, st.NAME as STATION_NAME, st.EXP_NO as STATION_NO, eq.DEVICE_NUM as DEVICE_NO, dt.COU, dt.PARMNAME, dt.DATATIME, dt.GUID "
				+ " from BC_SITE_INFO st, BC_COLLECTION_INFO eq, MONITOR_DATA dt "
				+ " where st.EXP_NO = eq.SITE_ID and eq.device_num = dt.mn and " + " st.WWTP_ID = '" + wwtpId + "' and "
				+ " dt.DATATIME >= to_date('" + startDateTime + "', 'yyyy-mm-dd hh24:mi:ss') and "
				+ " dt.DATATIME <= to_date('" + endDateTime + "', 'yyyy-mm-dd hh24:mi:ss') " + " order by dt.DATATIME ";

		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(strSQL, parm);
		outString(result);
	}

	// 获取污染物超标记录
	@RequestMapping(value = "/getWrwOverflowRcd")
	public void getWrwOverflowRcd(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String MONITOR_WWTP = RequestUnit.getString("MONITOR_WWTP");
		String MONITOR_STATION = RequestUnit.getString("MONITOR_STATION");
		String MONITOR_DEVICE = RequestUnit.getString("MONITOR_DEVICE");
		String WARNING_STATUS = RequestUnit.getString("WARNING_STATUS"); // 多个处理状态
		String POLLUTANT_TYPE = RequestUnit.getString("POLLUTANT_TYPE"); // 多种监测数据类型
		String START_TIME_FROM = RequestUnit.getString("START_TIME_FROM");
		String START_TIME_TO = RequestUnit.getString("START_TIME_TO");
		String END_TIME_FROM = RequestUnit.getString("END_TIME_FROM");
		String END_TIME_TO = RequestUnit.getString("END_TIME_TO");

		String[] arrPOLLUTANT_TYPE = POLLUTANT_TYPE.split(",");
		String[] arrWARNING_STATUS = WARNING_STATUS.split(",");
		String[] arrMONITOR_STATION = MONITOR_STATION.split(",");

		String strSQL = "select po.*, pw.NAME as WWTP_NAME, ms.name as STATION_NAME, me.NAME as DEVICE_NAME, dc1.s_domaincaption as POLLUTANT_NAME, dc2.s_domaincaption as STATUS_NAME from ( select * from POLLUTANT_OVERFLOW where ";

		if (MONITOR_WWTP != null && MONITOR_WWTP.length() > 0) {
			strSQL = strSQL + "MONITOR_WWTP = '" + MONITOR_WWTP + "' AND ";
		}

		if (MONITOR_DEVICE != null && MONITOR_DEVICE.length() > 0) {
			strSQL = strSQL + "MONITOR_DEVICE = '" + MONITOR_DEVICE + "' AND";
		}

		if (START_TIME_FROM != null && START_TIME_FROM.length() > 0) {
			strSQL = strSQL + "START_TIME >= to_date('" + START_TIME_FROM + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND ";
		}

		if (START_TIME_TO != null && START_TIME_TO.length() > 0) {
			strSQL = strSQL + "START_TIME <= to_date('" + START_TIME_TO + " 23:59:59', 'yyyy-mm-dd hh24:mi:ss') AND ";
		}

		if (END_TIME_FROM != null && END_TIME_FROM.length() > 0) {
			strSQL = strSQL + "END_TIME >= to_date('" + END_TIME_FROM + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND ";
		}

		if (END_TIME_TO != null && END_TIME_TO.length() > 0) {
			strSQL = strSQL + "END_TIME <= to_date('" + END_TIME_TO + " 23:59:59', 'yyyy-mm-dd hh24:mi:ss') AND ";
		}

		strSQL = strSQL + " ( ";

		for (int i = 0; i < arrPOLLUTANT_TYPE.length; i++) {
			if (arrPOLLUTANT_TYPE[i].length() < 1)
				continue;
			if (i < arrPOLLUTANT_TYPE.length - 1)
				strSQL = strSQL + "POLLUTANT_TYPE = '" + arrPOLLUTANT_TYPE[i] + "' OR ";
			else
				strSQL = strSQL + "POLLUTANT_TYPE = '" + arrPOLLUTANT_TYPE[i] + "' ";
		}

		strSQL = strSQL + " ) AND ( ";

		for (int i = 0; i < arrWARNING_STATUS.length; i++) {
			if (arrWARNING_STATUS[i].length() < 1)
				continue;
			if (i < arrWARNING_STATUS.length - 1)
				strSQL = strSQL + "WARNING_STATUS = '" + arrWARNING_STATUS[i] + "' OR ";
			else
				strSQL = strSQL + "WARNING_STATUS = '" + arrWARNING_STATUS[i] + "' ";
		}

		strSQL = strSQL + " ) AND ( ";

		for (int i = 0; i < arrMONITOR_STATION.length; i++) {
			if (arrMONITOR_STATION[i].length() < 1)
				continue;
			if (i < arrMONITOR_STATION.length - 1)
				strSQL = strSQL + "MONITOR_STATION = '" + arrMONITOR_STATION[i] + "' OR ";
			else
				strSQL = strSQL + "MONITOR_STATION = '" + arrMONITOR_STATION[i] + "'";
		}

		strSQL = strSQL + " ) ) po  left join BC_ENTERPRISE_ATTR pw on po.MONITOR_WWTP = pw.EXP_NO "
				+ " left join BC_SITE_DEVICE_RPT  me on po.MONITOR_DEVICE = me.DEVICE_NUM "
				+ " left join BC_SITE_INFO ms on po.MONITOR_STATION = ms.EXP_NO "
				+ " left join DATA_DEFULATDOMAIN dc1 on ( dc1.S_DOMAINNAME = 'MONITOR_PARAM' AND po.POLLUTANT_TYPE = dc1.S_DOMAINCODE ) "
				+ " left join DATA_DEFULATDOMAIN dc2 on ( dc2.S_DOMAINNAME = 'WARNING_STATUS' AND po.WARNING_STATUS = dc2.S_DOMAINCODE ) "
				+ " order by po.START_TIME DESC ";

		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(strSQL, parm);
		outString(result);
	}

	// 获取污染物超标处置记录
	@RequestMapping(value = "/getWrwOverflowSolveRcd")
	public void getWrwOverflowSolveRcd(HttpServletRequest resq, HttpServletResponse res) throws Exception {

		String POLLUTANT_OVERFLOW_NO = RequestUnit.getString("POLLUTANT_OVERFLOW_NO");
		// String STATUS = RequestUnit.getString("STATUS");

		String strSQL = "select * from POLLUTANT_OVERFLOW_SOLVE where POLLUTANT_OVERFLOW_NO='" + POLLUTANT_OVERFLOW_NO
				+ "'";
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		String result = DataUtil.getPageSelectJson(strSQL, parm);
		outString(result);
	}

	// 获取站点阀值(报表导出专用)
	public List<RowBase> getAlarmInfoForExport(String wwtp_id, String flow_type,String station_id) {
		String sql = "select DENSITY_ALARM_DOWMLIMIT down,DENSITY_ALARM_UPLIMIT up,sp.name from BC_SITE_POLL sp\n"
				+ "       left join BC_SITE_INFO ms on ms.exp_no = sp.site_id\n"
				+ "       where 1=1";
		
		List<GlobalParameter> listParam = new ArrayList<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			sql += " and ms.wwtp_id = @wwtp_id";
			listParam.add(new GlobalParameter("wwtp_id", wwtp_id));
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			sql += " and ms.flow_type = @flow_type";
			listParam.add(new GlobalParameter("flow_type", flow_type));
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			sql += " and ms.code = @station_id";
			listParam.add(new GlobalParameter("station_id", station_id));
		}

		List<RowBase> list = DataUtil.getSelectRows(sql, listParam);
		return list;
	}
	//根据参数获取上下限值
		@RequestMapping(value = "/getlimit")
		public void getlimit(HttpServletRequest resq, HttpServletResponse res) throws Exception {
			String stationId = RequestUnit.getString("stationId");
			String strSQL = "select name,DENSITY_ALARM_DOWMLIMIT,DENSITY_ALARM_UPLIMIT from BC_SITE_POLL where site_id='" + stationId+ "'";
			List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
			String result = DataUtil.getPageSelectJson(strSQL, parm);
			outString(result);
		}
}
